<?php
require_once __DIR__ . '/../database/db_connect.php';


function getStationNear(){
	$db = new DB_CONNECT();
	mysql_query("SET NAMES 'UTF8'"); // ok
	// get data request
	$lat = $_GET['lat_click'];
	$lon = $_GET['lon_click'];
	// get data from database

	$latlon = array();

	$item = array("u" => $lat + 0.0018, "d" => $lat - 0.0018, "l" => $lon - 0.0018, "r" => $lon + 0.0018); // 200m
	array_push($latlon, $item);

	$item = array("u" => $lat + 0.0027, "d" => $lat - 0.0027, "l" => $lon - 0.00275, "r" => $lon + 0.00275); // 300m
	array_push($latlon, $item);

	$item = array("u" => $lat + 0.0036, "d" => $lat - 0.0036, "l" => $lon - 0.00365, "r" => $lon + 0.00365); // 400m
	array_push($latlon, $item);
 
	$item = array("u" => $lat + 0.0045, "d" => $lat - 0.0045, "l" => $lon - 0.00455, "r" => $lon + 0.00455); // 500m
	array_push($latlon, $item);

	$data_respon = array();

	for($i = 0; $i < 4; $i++)
	{
		$cond = $latlon[$i];

		$d = $cond['d'];
		$u = $cond['u'];
		$l = $cond['l'];
		$r = $cond['r'];

		$getdata = mysql_query("SELECT * FROM stations WHERE lat >= $d AND lat <= $u AND lon >= $l AND lon <= $r");

		while ($row = mysql_fetch_assoc($getdata)) {
			array_push($data_respon, $row);
		}

		if(mysql_num_rows($getdata) > 0){
			break;
		}
	}
	echo json_encode($data_respon);
}
// call
getStationNear();
?>
